package com.yuanfengPlus.basicInfoManage.customerInfo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuanfengPlus.basicInfoManage.customerInfo.dao.ICustomerinfoDao;
import com.yuanfengPlus.basicInfoManage.customerInfo.model.CustomerDto;
import com.yuanfengPlus.basicInfoManage.customerInfo.model.Customerinfo;
import com.yuanfengPlus.basicInfoManage.customerInfo.service.ICustomerinfoservice;
import com.yuanfengPlus.utils.PageResult;
import com.yuanfengPlus.utils.Result;
import com.yuanfengPlus.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CustomerinfoserviceImpl implements ICustomerinfoservice {
    @Autowired
    ICustomerinfoDao icust;
    @Override
    public PageResult selectCustomerAll(CustomerDto customerDto) {
        PageHelper.startPage(customerDto.getPage(),customerDto.getPageSize());
        Page<Customerinfo> customerinfos = icust.selectCustomerAll(customerDto);
        return new PageResult(customerinfos.getTotal(),customerinfos.getResult());

    }

    @Override
    public Result<String> addCustomer(Customerinfo customerinfo) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0);
        customerinfo.setCustomer_id(idWorker.nextId());
        customerinfo.setCustomer_time(new java.sql.Date(System.currentTimeMillis()));
        customerinfo.setCustomer_amendtime(new java.sql.Date(System.currentTimeMillis()));
        Integer integer = icust.addCustomer(customerinfo);
        if (integer > 0){
            return Result.success("添加成功");
        }
        return Result.failure(400,"添加失败");
    }

    @Override
    public Result<String> updateCustomer(Customerinfo customerinfo) {
        Integer integer = icust.updateCustomer(customerinfo);
        if (integer > 0){
            return Result.success("修改成功");
        }
        return Result.failure(400,"修改失败");
    }

    @Override
    public Result<Customerinfo> selectCustomer(String id) {
        Customerinfo customerinfo = icust.selectCustomer(id);
        if (customerinfo != null){
            return Result.success(customerinfo);
        }
        return Result.failure(400,"没找到");
    }
}
